package com.gaimall.beans.pagebean;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.gaimall.beans.pagebean.element.BrandE;
import com.gaimall.beans.pagebean.element.DetailE;
import com.gaimall.beans.pagebean.element.SourceE;
import com.gaimall.beans.pagebean.element.TypeE;
import com.gaimall.beans.xmlbean.Goods;
import com.gaimall.beans.xmlbean.Result;

public class ResultPage {
	private String length;
	private String time;
	private List<BrandE> brand_list = new ArrayList<BrandE>();
	private List<TypeE> type_list = new ArrayList<TypeE>();
	private List<TypeE> type_list_hide = new ArrayList<TypeE>();
	private List<SourceE> source_list = new ArrayList<SourceE>();
	private List<SourceE> source_list_hide = new ArrayList<SourceE>();
	private List<DetailE> detail_list = new ArrayList<DetailE>();

	private String type_list_hide_css;
	private String source_list_hide_css;
	private String sorter_url_1;
	private String sorter_url_2;

	private final static Map<String, String> shopmap = new HashMap<String, String>();
	private final static String[] source_for_pics = {"360buy","suning","dangdang","coo8","gome","amazon","vancl","yihaodian","taobao"};

	public ResultPage(Result r, String kw, String page, String sorter, String r_type,
			String r_brand, String r_source) {

		this();
		if (r.getLength().equals("0")) {
			return;
		}
		String url = "/gaimall/search?kw=" + kw + "&page=" + 1 + "&sorter=" + sorter;

		// fill length and time;
		this.length = r.getLength();
		this.time = r.getTime();

		// fill brand_list
		String[] r_b = r.getFilter().getBrand_same().split(",");
		for (String e : r_b) {
			StringBuffer url_e = new StringBuffer(url);
			String[] el = e.split("=");
			BrandE be = new BrandE();

			if (el.length > 2) {
				be.setName(el[0] + "=" + el[1]);
				be.setAmount(Integer.parseInt(el[2]));
			} else {
				be.setName(el[0]);
				be.setAmount(Integer.parseInt(el[1]));
			}
			if (r_type != null && !"".equals(r_type)) {
				url_e.append("&type=" + r_type);
			}
			
			if(!el[0].replace("/", ",").replace(" ", ",").equals(r_brand)){
				url_e.append("&brand=" + el[0].replace("/", ",").replace(" ", ","));
			}else{
				be.setStyle("style=\"color:#FF8400\"");
			}
			
			if (r_source != null && !"".equals(r_source)) {
				url_e.append("&source=" + r_source);
			}
			be.setUrl(url_e.toString());
			this.brand_list.add(be);
		}

		// fill type_list
		String[] r_t = r.getFilter().getType_same().split(",");
		int r_t_length = r_t.length;
		if (r_t_length > 10) {
			for (int i = 0; i < 10; i++) {
				StringBuffer url_e = new StringBuffer(url);
				String[] el = r_t[i].split("=");
				TypeE te = new TypeE();
				te.setName(el[0]);
				te.setAmount(Integer.parseInt(el[1]));
				
				if(!el[0].equals(r_type)){
					url_e.append("&type=" + el[0]);
				}else{
					te.setStyle("style=\"color:#FF8400\"");
				}
				if (r_brand != null && !"".equals(r_brand)) {
					url_e.append("&brand=" + r_brand);
				}
				if (r_source != null && !"".equals(r_source)) {
					url_e.append("&source=" + r_source);
				}
				te.setUrl(url_e.toString());
				this.type_list.add(te);
			}

			for (int i = 10; i < r_t_length; i++) {
				StringBuffer url_e = new StringBuffer(url);
				String[] el = r_t[i].split("=");
				TypeE te = new TypeE();
				te.setName(el[0]);
				te.setAmount(Integer.parseInt(el[1]));
				if(!el[0].equals(r_type)){
					url_e.append("&type=" + el[0]);
				}else{
					te.setStyle("style=\"color:#FF8400\"");
				}
				if (r_brand != null && !"".equals(r_brand)) {
					url_e.append("&brand=" + r_brand);
				}
				if (r_source != null && !"".equals(r_source)) {
					url_e.append("&source=" + r_source);
				}

				te.setUrl(url_e.toString());
				this.type_list_hide.add(te);
			}

		} else {
			for (String e : r_t) {
				StringBuffer url_e = new StringBuffer(url);
				String[] el = e.split("=");
				TypeE te = new TypeE();
				te.setName(el[0]);
				te.setAmount(Integer.parseInt(el[1]));
				if(!el[0].equals(r_type)){
					url_e.append("&type=" + el[0]);
				}else{
					te.setStyle("style=\"color:#FF8400\"");
				}
				if (r_brand != null && !"".equals(r_brand)) {
					url_e.append("&brand=" + r_brand);
				}
				if (r_source != null && !"".equals(r_source)) {
					url_e.append("&source=" + r_source);
				}

				te.setUrl(url_e.toString());
				this.type_list.add(te);
			}
		}

		// fill source_list
		List<String> source_fc_groupby = Split_and_groupby(r.getFilter().getSource_fc());
		int r_s_length = source_fc_groupby.size();
		if (r_s_length > 10) {
			for (int i = 0; i < 10; i++) {
				StringBuffer url_e = new StringBuffer(url);
				String[] el = source_fc_groupby.get(i).split("\\(");
				SourceE se = new SourceE();
				se.setName(el[0]);
				se.setCn_name(shopmap.get(el[0]));
				se.setAmount(Integer.parseInt(el[1].split("\\)")[0]));
				if (r_type != null && !"".equals(r_type)) {
					url_e.append("&type=" + r_type);
				}
				if (r_brand != null && !"".equals(r_brand)) {
					url_e.append("&brand=" + r_brand);
				}
				if(!el[0].equals(r_source)){
					url_e.append("&source=" + el[0]);
				}else{
					se.setStyle("style=\"color:#FF8400\"");
				}
				se.setUrl(url_e.toString());
				this.source_list.add(se);
			}

			for (int i = 10; i < r_s_length; i++) {
				StringBuffer url_e = new StringBuffer(url);
				String[] el = source_fc_groupby.get(i).split("\\(");
				SourceE se = new SourceE();
				se.setName(el[0]);
				se.setCn_name(shopmap.get(el[0]));
				se.setAmount(Integer.parseInt(el[1].split("\\)")[0]));

				if (r_type != null && !"".equals(r_type)) {
					url_e.append("&type=" + r_type);
				}
				if (r_brand != null && !"".equals(r_brand)) {
					url_e.append("&brand=" + r_brand);
				}
				if(!el[0].equals(r_source)){
					url_e.append("&source=" + el[0]);
				}else{
					se.setStyle("style=\"color:#FF8400\"");
				}
				se.setUrl(url_e.toString());
				this.source_list_hide.add(se);
			}
		} else {
			for (String e : source_fc_groupby) {
				StringBuffer url_e = new StringBuffer(url);
				String[] el = e.split("\\(");
				SourceE se = new SourceE();
				se.setName(el[0]);
				se.setCn_name(shopmap.get(el[0]));
				se.setAmount(Integer.parseInt(el[1].split("\\)")[0]));

				if (r_type != null && !"".equals(r_type)) {
					url_e.append("&type=" + r_type);
				}
				if (r_brand != null && !"".equals(r_brand)) {
					url_e.append("&brand=" + r_brand);
				}
				if(!el[0].equals(r_source)){
					url_e.append("&source=" + el[0]);
				}else{
					se.setStyle("style=\"color:#FF8400\"");
				}
				se.setUrl(url_e.toString());
				this.source_list.add(se);
			}
		}

		// fill detail_list
		List<Goods> goods_list = r.getGoods_list();
		for (Goods e : goods_list) {
			DetailE de = new DetailE();
			de.setUrl("/gaimall/item?id=" + e.getId() + "&page=1");
			de.setName(e.getStandard__name());
			String[] goods_source = e.getSource().split("어",-1);
			String[] goods_pic_url = e.getPic__url().split("어",-1);

			de.setShop_amount(String.valueOf(goods_source.length));

			if ((goods_source.length != goods_pic_url.length) || goods_pic_url.length == 0) {
				de.setPic_url("http://www.fmcorn.com/static/shop/nopic.jpg");
			} else {
				// TODO item图片选择
				String matched_s ="";
				for(String sfpe :source_for_pics){
					if(e.getSource().indexOf(sfpe)>=0){
						matched_s = sfpe;
						break;
					}
				}
				
				if("".equals(matched_s)){
					de.setPic_url(goods_pic_url[goods_pic_url.length-1]);
				}else{
					for(int i=0;i<goods_source.length;i++){
						if(goods_source[i].indexOf(matched_s)>=0){
							de.setPic_url(goods_pic_url[i]);
							break;
						}
					}
				}
			}
			String[] prices = e.getPrice().split("어");

			try {
				int price = Integer.parseInt(prices[0].split("\\.")[0].replace(",", ""));
				for (String p_e : prices) {
					int temp_price = Integer.parseInt(p_e.split("\\.")[0].replace(",", ""));
					if (temp_price < price) {
						price = temp_price;
					}
				}
				de.setPrice(String.valueOf(price) + "元");
			} catch (NumberFormatException nfe) {
				de.setPrice("暂无报价");
			}
			detail_list.add(de);
		}

		// add css
		int tlhs = type_list_hide.size();
		int t_top = tlhs > 0 ? (((tlhs + 3) / 4) * 30 + 22 - 30) / 2 : 0;

		int slhs = source_list_hide.size();
		int s_top = slhs > 0 ? (((slhs + 3) / 4) * 30 + 22 - 30) / 2 : 0;

		if (t_top == 0) {
			type_list_hide_css = "";
		} else {
			type_list_hide_css = "style=\"top: -" + t_top + "px; \"";
		}

		if (s_top == 0) {
			source_list_hide_css = "";
		} else {
			source_list_hide_css = "style=\"top: -" + s_top + "px; \"";
		}

		// add sort url
		StringBuffer sort_sb = new StringBuffer("/gaimall/search?kw=" + kw + "&page=" + 1);
		if (r_type != null && !"".equals(r_type)) {
			sort_sb.append("&type=" + r_type);
		}
		if (r_brand != null && !"".equals(r_brand)) {
			sort_sb.append("&brand=" + r_brand);
		}
		if (r_source != null && !"".equals(r_source)) {
			sort_sb.append("&source=" + r_source);
		}

		if ("1".equals(sorter)) {
			this.sorter_url_1 = "<a href=\"" + sort_sb.toString() + "&sorter=1"
					+ "\" class=\"btn sort clicked\">默认排序</a>";
			this.sorter_url_2 = "<a href=\"" + sort_sb.toString() + "&sorter=2"
					+ "\" class=\"btn sort\">销量</a>";
		} else {
			this.sorter_url_1 = "<a href=\"" + sort_sb.toString() + "&sorter=1"
					+ "\" class=\"btn sort\">默认排序</a>";
			this.sorter_url_2 = "<a href=\"" + sort_sb.toString() + "&sorter=2"
					+ "\" class=\"btn sort clicked\">销量</a>";
		}

	}

	public ResultPage() {
		shopmap.put("amazon", "亚马逊");
		shopmap.put("binggo", "缤购");
		shopmap.put("caomeipai", "草莓派");
		shopmap.put("chinadrtv", "橡果国际");
		shopmap.put("cm365", "成名网");
		shopmap.put("coo8", "库巴购物网");
		shopmap.put("cosmede", "玫丽网");
		shopmap.put("dangdang", "当当网");
		shopmap.put("dao", "道网");
		shopmap.put("dwgou", "道位购");
		shopmap.put("easy361", "易斯来福商城");
		shopmap.put("efeihu", "飞虎乐购");
		shopmap.put("gome", "国美电器");
		shopmap.put("goodjd", "品牌家电网");
		shopmap.put("heroone", "华英雄");
		shopmap.put("jumei", "聚美");
		shopmap.put("lafaso", "乐蜂");
		shopmap.put("leftlady", "左岸女人");
		shopmap.put("likeface", "likeface");
		shopmap.put("liqunshop", "利群商城");
		shopmap.put("lusen", "绿森数码");
		shopmap.put("m18", "麦考林");
		shopmap.put("mobil89", "移宝网");
		shopmap.put("moonbasa", "梦芭莎");
		shopmap.put("new7", "新七天电器");
		shopmap.put("newegg", "新蛋购物");
		shopmap.put("no5", "No5");
		shopmap.put("ocj", "东方cj");
		shopmap.put("orange3c", "品橙网");
		shopmap.put("ouku", "欧酷");
		shopmap.put("shjdq", "盛家电器网");
		shopmap.put("shnn", "申网商城");
		shopmap.put("shougoo", "首购网");
		shopmap.put("suning", "苏宁易购");
		shopmap.put("szjdw", "深圳家电网");
		shopmap.put("taobao", "天猫");
		shopmap.put("vancl", "凡客");
		shopmap.put("vjia", "vjia");
		shopmap.put("w139shop", "北斗星手机网");
		shopmap.put("w360buy", "京东商城");
		shopmap.put("w51buy", "易讯网");
		shopmap.put("w51ppll", "漂亮无忧");
		shopmap.put("w529buy", "鸿信通");
		shopmap.put("xiu", "走秀网");
		shopmap.put("yidianda", "一点达");
		shopmap.put("yihaodian", "1号店");
		shopmap.put("yingtao", "樱桃网");
		shopmap.put("youpin", "悠品");
		shopmap.put("zhongyu", "中域商城");
		shopmap.put("redbaby", "红孩子");
		shopmap.put("muyingzhijia", "母婴之家");
		shopmap.put("m6go", "麦乐购");
		shopmap.put("lvhezi", "绿盒子");
		shopmap.put("aiyingshi", "爱婴室");
		shopmap.put("leyou", "乐友");
		shopmap.put("qinqinbaby", "亲亲宝贝");
		shopmap.put("boshiwa", "博士蛙");
		shopmap.put("lijiababy", "丽家宝贝");
		shopmap.put("minitiao", "迷你挑");
		shopmap.put("tbb", "特宝贝");
		shopmap.put("wbiao", "万表网");
		shopmap.put("ppbiao", "翡表网");
		shopmap.put("ctf", "周大福");
		shopmap.put("yintai", "银泰网");
		shopmap.put("yoho", "有货");
		shopmap.put("mfplaza", "马克华菲");
		shopmap.put("luckcart", "热客");
		shopmap.put("w360mart", "买特网");
		shopmap.put("uiyi", "佑一良品");
		shopmap.put("letao", "乐淘");
		shopmap.put("htjz", "胡桃夹子");
		shopmap.put("lamiu", "兰缪");
		shopmap.put("menglu", "梦露时尚");
		shopmap.put("aimer", "爱慕");
		shopmap.put("vingz", "薇哲");
		shopmap.put("mbaobao", "麦包包");
		shopmap.put("xzuan", "新钻网");
		shopmap.put("jufengshang", "聚风尚");
		shopmap.put("w5lux", "第五大道");
		shopmap.put("d1", "d1优尚");
		shopmap.put("sealbeauty", "随尔美");
		shopmap.put("s", "名鞋库");
		shopmap.put("dazhe", "名品打折网");
		shopmap.put("yougou", "优购网");
		shopmap.put("taoxie", "淘鞋网");
		shopmap.put("paixie", "拍鞋网");
		shopmap.put("vcotton", "维棉");
		shopmap.put("kela", "珂兰钻石");
		shopmap.put("dionly", "戴欧妮");
		shopmap.put("okbuy", "好乐买");
		shopmap.put("w520", "戴维尼珠宝网");
		shopmap.put("onlyts", "OnlyTS");
		shopmap.put("daphne", "达芙妮");
		shopmap.put("aidai", "爱戴网");
		shopmap.put("yjcs", "亿超眼镜");
		shopmap.put("meijing", "美睛网");
		shopmap.put("sigo", "视客眼镜网");
		shopmap.put("easeeyes", "易视网");
		shopmap.put("myeyes", "爱视商城");
		shopmap.put("jiuxian", "酒仙网");
		shopmap.put("wine9", "品尚红酒");
		shopmap.put("yesmywine", "也买酒");
		shopmap.put("madeinworld", "美德因我");
		shopmap.put("winekee", "红酒客");
		shopmap.put("womai", "我买网");
		shopmap.put("winenice", "酒美网");
		shopmap.put("lepinjiu", "乐品酒");
		shopmap.put("w12580wine", "12580红酒");
		shopmap.put("sdwh", "时代万货");
	}

	/**
	 * 对Source String 进一步处理的方法，能将xml中的source内容group。
	 * 
	 * @param s
	 * @return
	 */
	private List<String> Split_and_groupby(String s) {
		String[] l = s.split(",");
		ArrayList<String> al = new ArrayList<String>();
		Map<String, Integer> m = new HashMap<String, Integer>();
		for (String e : l) {
			String[] es = e.split("=");
			String name = es[0].split("_")[0];
			int count = Integer.parseInt(es[1]);
			if (m.containsKey(name)) {
				int n = m.get(name);
				m.put(name, n + count);
			} else {
				m.put(name, count);
			}
		}
		MyComparator bvc = new MyComparator(m);
		TreeMap<String, Integer> sorted_map = new TreeMap<String, Integer>(bvc);
		sorted_map.putAll(m);
		for (Map.Entry<String, Integer> entry : sorted_map.entrySet()) {
			al.add(entry.getKey() + "(" + entry.getValue() + ")");
		}
		return al;
	}

	class MyComparator implements Comparator<String> {
		Map<String, Integer> base;

		public MyComparator(Map<String, Integer> base) {
			this.base = base;
		}

		@Override
		public int compare(String a, String b) {
			if (base.get(a) >= base.get(b)) {
				return -1;
			} else {
				return 1;
			}
		}

	}

	public List<BrandE> getBrand_list() {
		return brand_list;
	}

	public void setBrand_list(List<BrandE> brand_list) {
		this.brand_list = brand_list;
	}

	public List<TypeE> getType_list() {
		return type_list;
	}

	public void setType_list(List<TypeE> type_list) {
		this.type_list = type_list;
	}

	public List<TypeE> getType_list_hide() {
		return type_list_hide;
	}

	public void setType_list_hide(List<TypeE> type_list_hide) {
		this.type_list_hide = type_list_hide;
	}

	public List<SourceE> getSource_list() {
		return source_list;
	}

	public void setSource_list(List<SourceE> source_list) {
		this.source_list = source_list;
	}

	public List<SourceE> getSource_list_hide() {
		return source_list_hide;
	}

	public void setSource_list_hide(List<SourceE> source_list_hide) {
		this.source_list_hide = source_list_hide;
	}

	public List<DetailE> getDetail_list() {
		return detail_list;
	}

	public void setDetail_list(List<DetailE> detail_list) {
		this.detail_list = detail_list;
	}

	public String getLength() {
		return length;
	}

	public void setLength(String length) {
		this.length = length;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public String getType_list_hide_css() {
		return type_list_hide_css;
	}

	public void setType_list_hide_css(String type_list_hide_css) {
		this.type_list_hide_css = type_list_hide_css;
	}

	public String getSource_list_hide_css() {
		return source_list_hide_css;
	}

	public void setSource_list_hide_css(String source_list_hide_css) {
		this.source_list_hide_css = source_list_hide_css;
	}

	public String getSorter_url_1() {
		return sorter_url_1;
	}

	public String getSorter_url_2() {
		return sorter_url_2;
	}

}
